package com.budgetme.ui.fragments;
 
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseIntArray;
import android.view.ViewGroup;
 
import java.util.HashMap;

 
public class CursorFragmentAdapter extends FragmentStatePagerAdapter {
 
	protected boolean mDataValid;
	protected Cursor mCursor;
	protected Context mContext;
	protected SparseIntArray mItemPositions;
	protected HashMap<Object, Integer> mObjectMap;
	protected int mRowIDColumn;
	
	private final String mFragment;
 
	public CursorFragmentAdapter(Activity context, FragmentManager fm, String fragment, Cursor cursor) {
		super(fm);
		
		mFragment = fragment;
		
		mObjectMap = new HashMap<Object, Integer>();
		boolean cursorPresent = cursor != null;
		mCursor = cursor;
		mDataValid = cursorPresent;
		mContext = context;
		mRowIDColumn = cursorPresent ? cursor.getColumnIndexOrThrow("_id") : -1;
		
		setItemPositions();

	}
 
	public Cursor getCursor() {
		return mCursor;
	}
 
	@Override
	public int getItemPosition(Object object) {
		Integer rowId = mObjectMap.get(object);
		if (rowId != null && mItemPositions != null) {
			return mItemPositions.get(rowId, POSITION_NONE);
		}
		return POSITION_NONE;
	}
 
	public void setItemPositions() {
		mItemPositions = null;
 
		if (mDataValid) {
			int count = mCursor.getCount();
			mItemPositions = new SparseIntArray(count);
			mCursor.moveToPosition(-1);
			while (mCursor.moveToNext()) {
				int rowId = mCursor.getInt(mRowIDColumn);
				int cursorPos = mCursor.getPosition();
				mItemPositions.append(rowId, cursorPos);
			}
		}
	}
 
	@Override
	public Fragment getItem(int position) {
		if (mDataValid) {
			mCursor.moveToPosition(position);
			Bundle args = new Bundle();
    		args.putInt(DetailsFragment.ARG_ID, mCursor.getInt(mRowIDColumn));

			return Fragment.instantiate(mContext, mFragment, args);
			
		} else {
			return null;
		}
	}
 
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		mObjectMap.remove(object);
 
		super.destroyItem(container, position, object);
	}
 
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		if (!mDataValid) {
			throw new IllegalStateException("this should only be called when the cursor is valid");
		}
		if (!mCursor.moveToPosition(position)) {
			throw new IllegalStateException("couldn't move cursor to position " + position);
		}
 
		int rowId = mCursor.getInt(mRowIDColumn);
		Object obj = super.instantiateItem(container, position);
		mObjectMap.put(obj, Integer.valueOf(rowId));
 
		return obj;
	}
  
	@Override
	public int getCount() {
		if (mDataValid) {
			return mCursor.getCount();
		} else {
			return 0;
		}
	}
 
	public void changeCursor(Cursor cursor) {
		Cursor old = swapCursor(cursor);
		if (old != null) {
			old.close();
		}
	}
 
	public Cursor swapCursor(Cursor newCursor) {
		if (newCursor == mCursor) {
			return null;
		}
		Cursor oldCursor = mCursor;
		mCursor = newCursor;
		if (newCursor != null) {
			mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
			mDataValid = true;
		} else {
			mRowIDColumn = -1;
			mDataValid = false;
		}
 
		setItemPositions();
		notifyDataSetChanged();
 
		return oldCursor;
	}
	
    public void setCurrent(int id, ViewPager pager) {

    	pager.setCurrentItem(mItemPositions.get(id, 0));

    }
 
}